<?php

namespace app\store\controller\wxapp;

use app\store\controller\Controller;
use app\store\model\WxappNav as WxappNavModel;

/**
 * 小程序帮助中心
 * Class Nav
 * @package app\store\controller\wxapp
 */
class Nav extends Controller
{
    /**
     * 帮助中心列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new WxappNavModel;
    $list = $model->getList();
        return $this->fetch('index', compact('list'));
   
        
    }


    /**
     * 添加帮助
     * @return array|mixed
     */
    public function add()
    {
        $model = new WxappNavModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('nav'))) {
            return $this->renderSuccess('添加成功', url('wxapp.nav/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新帮助
     * @param $id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        // 帮助详情
        $model = WxappNavModel::detail($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('nav'))) {
            return $this->renderSuccess('更新成功', url('wxapp.nav/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除帮助
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        // 帮助详情
        $model = WxappNavModel::detail($id);
        if (!$model->remove()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
